<template>
  <el-drawer
    title="添加标签"
    size="60%"
    :visible.sync="drawerVisible"
    direction="rtl"
    :close-on-press-escape="false"
    :wrapperClosable="false"
    :destroy-on-close="true"
    :before-close="beforeClose"
    custom-class="tag-drawer category-tag-drawer"
    v-drawerResize
  >
    <div class="drawer-wrap">
      <div class="drawer-content tag-scrollbar">
        <el-row>
          <el-col :span="12">
            <div class="list-wrapper">
              <div class="list-box">
                <tag-content-header>
                  <template #title>
                    <span>候选标签列表</span>
                  </template>
                </tag-content-header>
                <div class="filter">
                  <el-button-group class="search-box" :class="{ show: !showSearchBox }">
                    <el-button
                      type="primary"
                      size="mini"
                      @click="active = 'mount'"
                      plain
                      :class="{ active: active === 'mount' }"
                      >全量标签</el-button
                    >
                    <el-button
                      type="primary"
                      size="mini"
                      @click="active = 'month'"
                      plain
                      :class="{ active: active === 'month' }"
                      >本月新增标签</el-button
                    >
                    <el-button
                      type="primary"
                      size="mini"
                      @click="active = 'unmount'"
                      plain
                      :class="{ active: active === 'unmount' }"
                      >未分类标签</el-button
                    >
                  </el-button-group>
                  <el-input
                    v-model="params.queryParams.tagNm"
                    placeholder="请输入标签名称"
                    class="search-box"
                    :class="{ show: showSearchBox }"
                    @keyup.native.13="search"
                    clearable
                    @clear="search"
                  ></el-input>
                  <i class="el-icon-search" @click="showSearchBox = true" v-if="!showSearchBox"></i>
                  <i class="el-icon-search" @click="search" v-if="showSearchBox"></i>
                  <i class="el-icon-d-arrow-left" @click="showSearchBox = false" v-if="showSearchBox"></i>
                </div>
              </div>
              <div class="list-content unselect" v-loading.lock="loading">
                <ul class="list tag-scrollbar">
                  <li v-for="item in list" :key="item.tagId">
                    <div class="list-item">{{ item.tagNm }}</div>
                    <span class="tip" v-if="item.mounterId">已挂载</span>
                    <span class="tip select-tip" v-else-if="alreadySelect(item.tagId)">已选择</span>
                    <el-button
                      icon="el-icon-plus"
                      size="mini"
                      class="add-btn"
                      plain
                      circle
                      v-else
                      @click="addTag(item)"
                    ></el-button>
                  </li>
                </ul>
                <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="params.currentPageNum"
                  :page-sizes="[10, 15, 20, 30]"
                  :page-size="params.pageSize"
                  :pager-count="5"
                  background
                  layout="total, prev, pager, next, sizes"
                  small
                  :total="totalSize"
                >
                </el-pagination>
              </div>
            </div>
          </el-col>
          <el-col :span="1" class="add-icon"><i class="el-icon-d-arrow-right"></i></el-col>
          <el-col :span="11">
            <div class="list-wrapper">
              <div class="list-box">
                <tag-content-header>
                  <template #title>
                    <span>已选标签列表</span>
                  </template>
                </tag-content-header>
              </div>
              <div class="list-content">
                <ul class="list tag-scrollbar">
                  <li v-for="item in selectedList" :key="item.tagId">
                    {{ item.tagNm }}
                    <el-button icon="el-icon-close" size="mini" plain circle @click="removeTag(item)"></el-button>
                  </li>
                </ul>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <div class="drawer-btns">
      <el-button size="medium" @click="beforeClose()">取消</el-button>
      <el-button type="primary" size="medium" @click="confirm">确定</el-button>
    </div>
  </el-drawer>
</template>

<script>
export default {
  props: {
    catalogId: {
      type: [String, Number],
      required: true,
      default: ''
    }
  },
  data() {
    return {
      drawerVisible: false,
      active: 'mount',
      loading: false,
      list: [],
      selectedList: [],
      showSearchBox: false,
      params: {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {
          tagNm: ''
        },
        descPriority: false
      },
      totalSize: 0
    }
  },
  watch: {
    active() {
      this.params = {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {
          tagNm: ''
        },
        descPriority: false
      }
      this.getList()
    },
    showSearchBox() {
      this.params = {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {
          tagNm: ''
        },
        descPriority: false
      }
      this.getList()
    },
    total() {
      let maxPageNum = Math.ceil(this.total / this.params.pageSize) || 1
      if (this.params.currentPageNum > maxPageNum) {
        this.params.currentPageNum = maxPageNum
        this.getList()
      }
    }
  },
  methods: {
    showDrawer() {
      this.drawerVisible = true
      this.getList()
    },
    handleSizeChange(val) {
      this.params.currentPageNum = 1
      this.params.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.params.currentPageNum = val
      this.getList()
    },
    getList() {
      this.loading = true
      this.params.queryParams.catalogId = this.catalogId
      this.$ajax({
        url: this.$ajax.setUrl(`dcl/dclAssetCatalogMount/${this.active}/pageList`),
        method: 'get',
        params: this.$ajax.setParams(this.params)
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.list = data.data.records
          this.totalSize = data.data.totalSize
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.loading = false
      })
    },
    search() {
      this.params.currentPageNum = 1
      this.getList()
    },
    addTag(item) {
      if (this.selectedList.find(val => val.tagId === item.tagId)) return
      this.selectedList.push(item)
    },
    removeTag(item) {
      let index = this.selectedList.findIndex(v => v.tagId === item.tagId)
      if (index >= 0) this.selectedList.splice(index, 1)
    },
    alreadySelect(id) {
      return this.selectedList.find(item => item.tagId === id)
    },
    beforeClose() {
      this.list = []
      this.selectedList = []
      this.active = 'mount'
      ;(this.params = {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      }),
        (this.totalSize = 0)
      this.drawerVisible = false
    },
    confirm() {
      if (!this.selectedList.length) {
        this.$confirm('未选择挂载资产，确认退出？').then(() => this.beforeClose())
        return
      } else {
        let assetIds = []
        this.selectedList.forEach(v => assetIds.push(v.tagId))
        this.$ajax({
          url: this.$ajax.setUrl('dcl/dclAssetCatalogMount/batch'),
          method: 'post',
          data: this.$ajax.setData({
            catalogId: this.catalogId,
            assetIds
          })
        }).then(({ data }) => {
          if (data && data.code === '0') {
            this.$message.success(data.data || data.msg)
            this.$emit('refresh')
            this.beforeClose()
          } else {
            this.$message.error(data.data || data.msg)
          }
        })
      }
    }
  }
}
</script>

<style lang="scss">
.category-tag-drawer {
  .el-row,
  .el-col,
  .list-wrapper {
    height: 100%;
  }
  .el-col {
    position: relative;
  }
  .add-icon i {
    font-size: 25px;
    color: $theme-color;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .list-wrapper {
    padding: 20px;
    position: relative;
    .list-box {
      height: 100%;
      border: 1px solid $theme-color;
      border-radius: 5px;
      .tag-content-header {
        margin: 0;
        padding: 0 20px;
        border-bottom: 1px solid $border-color;
        .title-box {
          height: 40px;
          line-height: 40px;
        }
      }
    }
    .filter {
      height: 50px;
      padding: 10px 0;
      text-align: center;
      .el-button.active {
        color: #fff;
        background-color: $theme-color;
      }
      i {
        font-size: 16px;
        color: $theme-color;
        margin-left: 10px;
        cursor: pointer;
      }
      .search-box {
        display: inline-block;
        width: 0;
        height: 0;
        vertical-align: middle;
        transition: width linear 0.3s;
        overflow: hidden;
        &.show {
          width: 275px;
          height: 30px;
        }
        .el-input__inner {
          height: 30px;
          line-height: 30px;
        }
        .el-input__suffix {
          top: -5px;
        }
      }
    }
    .list-content {
      position: absolute;
      top: 60px;
      right: 20px;
      bottom: 20px;
      left: 20px;
      overflow-y: auto;
      ul.list {
        height: 100%;
        overflow-y: auto;
        li {
          padding: 0 50px 0 20px;
          line-height: 40px;
          position: relative;
          &:hover {
            color: #fff;
            background-color: #a2abd9;
            .el-button {
              display: block;
            }
          }
          .list-item {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          .tip {
            color: $theme-color;
          }
          .select-tip {
            color: #666;
          }
          .el-button {
            display: none;
            margin-top: 5px;
            cursor: pointer;
            position: absolute;
            top: 0;
            right: 10px;
            &:hover {
              color: #fff;
              background-color: $theme-color;
            }
          }
          .tip,
          .add-btn {
            position: absolute;
            top: 0;
            right: 10px;
          }
        }
      }
      .el-pagination {
        text-align: center;
      }
      &.unselect {
        top: 110px;
        ul {
          height: calc(100% - 35px);
          li .el-button {
            display: block;
          }
        }
      }
    }
  }
}
</style>
